#include "QScreenLabel.h"
#include "QScreenMonitorWidget.h"
#include "apiScreenControlSocketClient.h"
#include <QDebug>

MOUSEKEYEVENTOBJ g_qMouseKeyEventData;

QScreenLabel::QScreenLabel(QWidget* parent)
	: QLabel(parent), m_qMonitorWidget(nullptr)
{
	this->setAttribute(Qt::WA_PaintOnScreen, true);
	this->setMouseTracking(true);
}

QScreenLabel::~QScreenLabel()
{
}
void QScreenLabel::SetQScreenMonitorWidget(QScreenMonitorWidget* qMonitorWidget)
{
	m_qMonitorWidget = qMonitorWidget;
}
void QScreenLabel::paintEvent(QPaintEvent* event) {
}

QPaintEngine* QScreenLabel::paintEngine() const {
	return 0;
}

void QScreenLabel::mousePressEvent(QMouseEvent* event) {
	if (ExistsSocketClient(m_qMonitorWidget->m_qScreenControlSocketClient)) {
		switch (event->button()) {
		case Qt::LeftButton:g_qMouseKeyEventData.type = EVENT_TYPE::MOUSE_L_DOWN; break;
		case Qt::RightButton:g_qMouseKeyEventData.type = EVENT_TYPE::MOUSE_R_DOWN; break;
		case Qt::MiddleButton:g_qMouseKeyEventData.type = EVENT_TYPE::MOUSE_M_DOWN; break;
		default:break;
		}
		SendMouseKeyCommand(m_qMonitorWidget->m_qScreenControlSocketClient.hSocketClient, g_qMouseKeyEventData);
	}
	event->accept();
}

void QScreenLabel::mouseReleaseEvent(QMouseEvent* event) {
	if (ExistsSocketClient(m_qMonitorWidget->m_qScreenControlSocketClient)) {
		switch (event->button()) {
		case Qt::LeftButton:g_qMouseKeyEventData.type = EVENT_TYPE::MOUSE_L_UP; break;
		case Qt::RightButton:g_qMouseKeyEventData.type = EVENT_TYPE::MOUSE_R_UP; break;
		case Qt::MiddleButton:g_qMouseKeyEventData.type = EVENT_TYPE::MOUSE_M_UP; break;
		default:break;
		}
		SendMouseKeyCommand(m_qMonitorWidget->m_qScreenControlSocketClient.hSocketClient, g_qMouseKeyEventData);
	}

	event->accept();
}
void QScreenLabel::mouseMoveEvent(QMouseEvent* event) {
	if (ExistsSocketClient(m_qMonitorWidget->m_qScreenControlSocketClient)) {
		const QPoint& point = event->pos();
		g_qMouseKeyEventData.type = EVENT_TYPE::MOUSE_MOVE;
		g_qMouseKeyEventData.x = point.x();
		g_qMouseKeyEventData.y = point.y();
		SendMouseKeyCommand(m_qMonitorWidget->m_qScreenControlSocketClient.hSocketClient, g_qMouseKeyEventData);
	}
	event->accept();
}

void QScreenLabel::mouseDoubleClickEvent(QMouseEvent* event) {
	//pressTimer.stop();
	//releaseTimer.stop();
	//if (eventQueue.size() > 1 && ExistsSocketClient(m_qMonitorWidget->m_qScreenControlSocketClient)) {
	//	eventQueue.dequeue();
	//	eventQueue.dequeue();
	//	dbks = 1;
	//	//qDebug() << QString("mouse dblck");
	//	switch (event->button()) {
	//	case Qt::LeftButton:g_qMouseKeyEventData.type = EVENT_TYPE::MOUSE_L_CLK;
	//		break;
	//	case Qt::RightButton:g_qMouseKeyEventData.type = EVENT_TYPE::MOUSE_R_CLK;
	//		break;
	//	default:break;
	//	}
	//	g_qMouseKeyEventData.x = 0;
	//	g_qMouseKeyEventData.y = 0;
	//	g_qMouseKeyEventData.ex = 0;
	//	emit ExistsSocketClient(m_qMonitorWidget->m_qScreenControlSocketClient)->eventTypeCatched(g_qMouseKeyEventData);
	//}
	//else if (eventQueue.size() > 0) {
	//	eventQueue.dequeue();

	//}
	//event->accept();
	QWidget::mouseDoubleClickEvent(event);
}
int translateKeyCode(int key);
void QScreenLabel::keyPressEvent(QKeyEvent* event)
{
	if (ExistsSocketClient(m_qMonitorWidget->m_qScreenControlSocketClient)) {
		g_qMouseKeyEventData.type = EVENT_TYPE::KEY_DOWN;
		g_qMouseKeyEventData.ex = translateKeyCode(event->key());
		SendMouseKeyCommand(m_qMonitorWidget->m_qScreenControlSocketClient.hSocketClient, g_qMouseKeyEventData);
	}
	if (event->key() != Qt::Key_Escape)
		event->accept();
	else
		QWidget::keyPressEvent(event);
}
void QScreenLabel::keyReleaseEvent(QKeyEvent* event) {
	if (ExistsSocketClient(m_qMonitorWidget->m_qScreenControlSocketClient)) {
		g_qMouseKeyEventData.type = EVENT_TYPE::KEY_UP;
		g_qMouseKeyEventData.ex = translateKeyCode(event->key());
		SendMouseKeyCommand(m_qMonitorWidget->m_qScreenControlSocketClient.hSocketClient, g_qMouseKeyEventData);
	}
	event->accept();
}

void QScreenLabel::wheelEvent(QWheelEvent* event) {
	if (ExistsSocketClient(m_qMonitorWidget->m_qScreenControlSocketClient)) {
		const QPoint& point = event->pos();
		g_qMouseKeyEventData.type = EVENT_TYPE::WHEEL;
		g_qMouseKeyEventData.x = point.x();
		g_qMouseKeyEventData.y = point.y();
		g_qMouseKeyEventData.ex = event->delta();
		SendMouseKeyCommand(m_qMonitorWidget->m_qScreenControlSocketClient.hSocketClient, g_qMouseKeyEventData);
	}
	event->accept();
}


int translateKeyCode(int key)
{
	int k = key;
	bool legal = true;
	if (k >= Qt::Key_0 && k <= Qt::Key_9)
	{
	}
	else if (k >= Qt::Key_A && k <= Qt::Key_Z)
	{
	}
	else if (k >= Qt::Key_F1 && k <= Qt::Key_F24)
	{
		k &= 0x000000ff;
		k += 0x40;
	}
	else if (k == Qt::Key_Tab)
	{
		k = 0x09;
	}
	else if (k == Qt::Key_Backspace)
	{
		k = 0x08;
	}
	else if (k == Qt::Key_Return)
	{
		k = 0x0d;
	}
	else if (k <= Qt::Key_Down && k >= Qt::Key_Left)
	{
		int off = k - Qt::Key_Left;
		k = 0x25 + off;
	}
	else if (k == Qt::Key_Shift)
	{
		k = 0x10;
	}
	else if (k == Qt::Key_Control)
	{
		k = 0x11;
	}
	else if (k == Qt::Key_Alt)
	{
		k = 0x12;
	}
	else if (k == Qt::Key_Meta)
	{
		k = 0x5b;
	}
	else if (k == Qt::Key_Insert)
	{
		k = 0x2d;
	}
	else if (k == Qt::Key_Delete)
	{
		k = 0x2e;
	}
	else if (k == Qt::Key_Home)
	{
		k = 0x24;
	}
	else if (k == Qt::Key_End)
	{
		k = 0x23;
	}
	else if (k == Qt::Key_PageUp)
	{
		k = 0x21;
	}
	else if (k == Qt::Key_Down)
	{
		k = 0x22;
	}
	else if (k == Qt::Key_CapsLock)
	{
		k = 0x14;
	}
	else if (k == Qt::Key_NumLock)
	{
		k = 0x90;
	}
	else if (k == Qt::Key_Space)
	{
		k = 0x20;
	}
	else
		legal = false;

	if (!legal)
		return 0;
	return k;
}
